﻿using Lcygcc.Domain.Repository.Entity;
using Lcygcc.Domain.Service;
using Lcygcc.Infrastructure.Logs;
using Lcygcc.Infrastructure.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lcygcc.Domain.ServiceImpl
{
    /// <summary>
    /// 数据清理服务类
    /// </summary>
    public class ClearDataService : IClearDataService
    {
        private readonly IBaseRepository mBaseRepository = null;
        public ClearDataService(IBaseRepository baseRepository)
        {
            mBaseRepository = baseRepository;
        }

        public void ClearSyncData()
        {
            //var list = mBaseRepository.db.Queryable<SyncTemp>().Where(it => it.SendNum <= 5).ToList();

            DateTime clearDataTime = DateTime.Now.AddDays(-15);
            //var clearSyncHistory1 = mBaseRepository.db.Queryable<SyncHistory>().Where(it => it.AddTime < clearDataTime).ToList();
            try
            {
                var clearSyncHistory = mBaseRepository.db.Deleteable<SyncHistory>().Where(it => it.AddTime < clearDataTime).ExecuteCommand();
                Logger.Info("-----ClearDataService.ClearSyncData.SyncHistory-----" + "成功清理" + clearSyncHistory + "条数据");
            }
            catch (Exception ex)
            {
                Logger.Error("-----ClearDataService.ClearSyncData.SyncHistory-----", ex);
            }

            try
            {
                clearDataTime = DateTime.Now.AddDays(-30);
                var clearSyncTemp = mBaseRepository.db.Deleteable<SyncTemp>().Where(it => it.AddTime < clearDataTime).ExecuteCommand();
                Logger.Info("-----ClearDataService.ClearSyncData.SyncTemp-----" + "成功清理" + clearSyncTemp + "条数据");
            }
            catch (Exception ex)
            {
                Logger.Error("-----ClearDataService.ClearSyncData.SyncTemp-----", ex);
            }
        }
    }
}
